﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapWASTPASS(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveWASTPASS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryWASTPASS(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapWASTPASS(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertWASTPASS(dwLocal);
            }
            UpdateWASTPASS(dwLocal);
        }
        public void InsertWASTPASS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO WASTPASS(WSPMANDANT, WSPKUNDNR, WSPWERK, WSPABT, WSPART, WSPVAR, WSPPOS, WSPPASS)"
                + "VALUES (:WSPMANDANT, '00000000', '   ', '   ', :WSPART, :WSPVAR, :WSPPOS,:WSPPASS)";
            or1.Parameters.Add("WSPMANDANT", OracleType.NVarChar).Value = dwData["WSPMANDANT"].ToString();
            or1.Parameters.Add("WSPART", OracleType.NVarChar).Value = dwData["WSPART"].ToString();
            or1.Parameters.Add("WSPVAR", OracleType.NVarChar).Value = dwData["WSPVAR"].ToString();
            or1.Parameters.Add("WSPPOS", OracleType.Number).Value = dwData["WSPPOS"].ToString();
            or1.Parameters.Add("WSPPASS", OracleType.NVarChar).Value = dwData["WSPPASS"].ToString();
            Connect2Proxy(or1);
        }
        public void DeleteWASTPASS(string client, string art, string var)
        {
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE WASTPASS WHERE   "
            + "  WSPMANDANT=:WSPMANDANT AND WSPART=:WSPART AND WSPVAR=:WSPVAR";
            or1.Parameters.Add("WSPMANDANT", OracleType.NVarChar).Value = client;
            or1.Parameters.Add("WSPART", OracleType.NVarChar).Value = art;
            or1.Parameters.Add("WSPVAR", OracleType.NVarChar).Value = var;
            Connect2Proxy(or1);
        }
        public DataSet QueryWASTPASS(DataRow dwData,int option=1)
        {
            DataSet set4Return = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WASTPASS WHERE  ROWNUM<2 ";
                    break;
                case 1: 
            or1.CommandText = "SELECT *FROM WASTPASS WHERE   "
            + "  WSPMANDANT=:WSPMANDANT AND WSPART=:WSPART AND WSPVAR=:WSPVAR";
            or1.Parameters.Add("WSPMANDANT", OracleType.NVarChar).Value = dwData["WSPMANDANT"].ToString();
            or1.Parameters.Add("WSPART", OracleType.NVarChar).Value = dwData["WSPART"].ToString();
            or1.Parameters.Add("WSPVAR", OracleType.NVarChar).Value = dwData["WSPVAR"].ToString();

            break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "WASTPASS";
            }
            set4Return.Merge(Result);
            return set4Return;
        }
        public void UpdateWASTPASS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE WASTPASS SET "
            + "WSPMANDANT=:WSPMANDANT,"
            + "WSPKUNDNR=:WSPKUNDNR,"
            + "WSPWERK=:WSPWERK,"
            + "WSPABT=:WSPABT,"
            + "WSPART=:WSPART,"
            + "WSPVAR=:WSPVAR,"
            + "WSPPOS=:WSPPOS,"
            + "WSPPASS=:WSPPASS "
            + " WHERE  "
            + "  WSPMANDANT=:WSPMANDANT AND WSPART=:WSPART AND WSPVAR=:WSPVAR AND WSPPOS=:WSPPOS";
            or1.Parameters.Add("WSPMANDANT", OracleType.NVarChar).Value = dwData["WSPMANDANT"].ToString();
            or1.Parameters.Add("WSPKUNDNR", OracleType.NVarChar).Value = dwData["WSPKUNDNR"].ToString();
            or1.Parameters.Add("WSPWERK", OracleType.NVarChar).Value = dwData["WSPWERK"].ToString();
            or1.Parameters.Add("WSPABT", OracleType.NVarChar).Value = dwData["WSPABT"].ToString();
            or1.Parameters.Add("WSPART", OracleType.NVarChar).Value = dwData["WSPART"].ToString();
            or1.Parameters.Add("WSPVAR", OracleType.NVarChar).Value = dwData["WSPVAR"].ToString();
            or1.Parameters.Add("WSPPOS", OracleType.NVarChar).Value = dwData["WSPPOS"].ToString();
            or1.Parameters.Add("WSPPASS", OracleType.NVarChar).Value = dwData["WSPPASS"].ToString();
            Connect2Proxy(or1);
        }
    }
}
